﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LIM.model;
using LIM.controller;

namespace LIM.forms.uc_CRM_LienHe_ChiTiet
{
    public partial class uc_CRM_LienHe_ChiTiet_ThongTinCongTy : UserControl
    {
        public uc_CRM_LienHe_ChiTiet_ThongTinCongTy()
        {
            InitializeComponent();

        }
        private void uc_CRM_LienHe_ChiTiet_ThongTinCongTy_Load(object sender, EventArgs e)
        {
            parentForm = (radForm_CRM_LienHe_ChiTiet)this.ParentForm;
            if (parentForm != null)
            {
                parentForm.OnLoadChange += new radForm_CRM_LienHe_ChiTiet.LoadChange(parentForm_OnRowChange);
            }
            BindingCompany.DataSource = parentForm.controller.LoadKhachHang();
            //BindingPhongBan.DataSource = parentForm.controller.LoadPhongBan();
            if (parentForm.SelectedEntity != null)
            {
                LoadData();
            }

        }
        void LoadData()
        {
            if (parentForm.SelectedEntity.RowID != 0)
            {
                radGridView_ThongTinLienHe.DataSource = parentForm.controller.LoadKhachHangLienHe(parentForm.SelectedEntity.RowID);
            }
            else
            {
                radGridView_ThongTinLienHe.DataSource = new List<tbl_CRM_LienHeKhachHang>();
            }
        }
        radForm_CRM_LienHe_ChiTiet parentForm = null;
        void parentForm_OnRowChange()
        {
            if (parentForm.SelectedEntity != null)
            {
                BindControls();
            }
        }
        protected void BindControls()
        {
            if (parentForm.SelectedEntity == null)
                parentForm.SelectedEntity = new tbl_CRM_LienHe();

        }

        private void radMenuItem_LuuLai_Click(object sender, EventArgs e)
        {
            Save();
        }
        void Save()
        {
            //parentForm.controller.DeleteLienHeKhachHang(parentForm.SelectedEntity.RowID);
            radGridView_ThongTinLienHe.EndEdit();
            for (int i = 0; i < radGridView_ThongTinLienHe.Rows.Count(); i++)
            {
                tbl_CRM_LienHeKhachHang item = (tbl_CRM_LienHeKhachHang)radGridView_ThongTinLienHe.Rows[i].DataBoundItem;
                int? IDKhahHang = null;
                if(radGridView_ThongTinLienHe.Rows[i].Cells[1].Value != null )
                {
                    IDKhahHang = int.Parse(radGridView_ThongTinLienHe.Rows[i].Cells[1].Value.ToString()) ;
                }
                item.RowIDKhachHang = IDKhahHang;
                if (item != null)
                {
                    if (item.RowIDKhachHang != null && item.PhongBan != null)
                    {
                        item.RowIDLienHe = parentForm.SelectedEntity.RowID;
                        if (item.RowID == 0)
                        {
                            parentForm.SelectedEntity.NguoiTao = InitUserInfos.UserName;
                            item.NgayTao = item.NgayCapNhat = parentForm.controller.GetCurrentDateTimeFromDatabaseServer();
                            parentForm.controller.SaveLienHeKhachHang(item);
                        }
                        else
                        {
                            item.NguoiCapNhat = InitUserInfos.UserName;
                            item.NgayCapNhat = parentForm.controller.GetCurrentDateTimeFromDatabaseServer();
                            
                        }
                        parentForm.controller.SaveLienHeKhachHang(item);
                    }
                }
            }
            RadDesktopAlert_SavingStatus.ShowAlertSaveSuccess("Liên hệ khách hàng", "Đã lưu thành công!");
            //parentForm.RefreshData();
            if (parentForm.SelectedEntity != null)
            {
                LoadData();
            }
        }

        private void radMenuItem_Refresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
